﻿using Dapper;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.DependencyInjection;
using YaAppoint.DappperConnection;

namespace YaAppoint.DapperRepositories
{
    public class DapperApplyDepartmentRepository(IConnectionProvider connectionProvider,
                                                 IConfiguration configuration ) : IDapperApplyDepartmentRepository, ITransientDependency
    {
        private readonly IConfiguration _configuration = configuration;
        private readonly IConnectionProvider _connectionProvider = connectionProvider;

        /// <summary>
        /// 获取申请科室
        /// </summary>
        /// <param name="InstitutionCode">医疗机构编码</param>
        public async Task<List<HisApplyDepartmentDto>> GetListAsync()
        {
            var databaseType = _connectionProvider.GetDatabaseType();
            var connStr = _configuration["ConnectionStrings:HisDb"] ;
            if (string.IsNullOrWhiteSpace(connStr))
            {
                throw new BusinessException(CustomErrorCodes.NoHisConnectionNotFund);
            }
            using var conn = _connectionProvider.GetDbConnection(connStr,databaseType);
            conn.Open();
            string condition = string.Empty;
            var sqlStr = $@"
                            select *  from pacs_view_apply_department
                         ";

            var res = (await conn.QueryAsync<HisApplyDepartmentDto>(sqlStr)).ToList();
            conn.Close();
            return res;
        }
    }
}
